<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
			</image>
			<text class="title text-black text-bold">先航科技</text>
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<!--      <view class="input-item flex align-center" v-if="captchaEnabled">-->
			<!--        <view class="iconfont icon-code icon"></view>-->
			<!--        <input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />-->
			<!--        <image :src="codeUrl" @click="getCode" class="login-code-img"></image>-->
			<!--      </view>-->
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
				<!-- <button @click="handleLogin11" class="login-btn cu-btn block bg-blue lg round">登录</button> -->
			</view>
			<view class="regist">
				<text class="text">您还没有账号？</text>
				<text class="text text-blue" @click="toRegister">立即注册</text>
			</view>

			<view class="xieyi text-center">
				<uni-data-checkbox style="display: inline-block" v-model="isCheck" :multiple="true" :localdata="range">
				</uni-data-checkbox>
				<text>我已阅读并接受</text>
				<text @click="handlePrivacy" class="text-blue">《用户服务及隐私协议》</text>
			</view>
		</view>
<!-- 		<Verify
		  @success="success"
		  :mode="'pop'"
		  captchaType="blockPuzzle"
		  :imgSize="{ width: '330px', height: '155px' }"
		  ref="verify"
		></Verify> -->
	</view>
</template>

<script>
	import {
		getCodeImg,
		getUnionId
	} from '@/api/login'

	export default {
		components: {
		},
		data() {
			return {
				codeUrl: "",
				captchaEnabled: true,
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "",
					password: "",
					code: "",
					uuid: '',
					jsCode: ''
				},
				isCheck: [],
				range: [{
					"value": 0
				}]
			}
		},
		created() {
			//this.getCode()
		},
		onLoad() {
			this.loginForm.username = uni.getStorageSync('username')
			this.loginForm.password = uni.getStorageSync('password')
		},
		mounted() {
			// wx.login({
			// 	success: function(res) {
			// 		console.log(res);
			// 		if (res.code) {
			// 			//就是在这里发起网络请求，使用wx.request()，将登录态发送给自家的服务器上
			// 			getUnionId(res.code).then(res => {
			// 			  console.log(res)
			// 			})
			// 			this.jsCode = res.code

			// 		} else {
			// 			console.log('获取用户登录态失败！' + res.errMsg)
			// 		}
			// 	},
			// 	fail: function() {},
			// 	complete: function() {}
			// });

		},
		methods: {
			handleLogin11(){
				wx.requestSubscribeMessage({
					 tmplIds: [
						"T8f38xqpAyU04ngfWD0hz_TNZT693km5fwTxnuDSPhg"
					 ],
					 success(res) {
						console.log("订阅消息API调⽤成功：",res)
					 },
					 fail(res) {
						console.log("订阅消息API调⽤失败：",res)
					 }
				 })
			},
			toRegister() {
				this.$tab.navigateTo("/pages/register");
			},
			// 隐私协议
			handlePrivacy() {
				this.$tab.navigateTo("/pages/privacy")
			},
			// 用户协议
			handleUserAgrement() {
				this.$tab.navigateTo("/pages/protocol")
			},
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.img
						this.loginForm.uuid = res.uuid
					}
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入您的密码")
				}
				// else if (this.loginForm.code === "" && this.captchaEnabled) {
				//   this.$modal.msgError("请输入验证码")
				// }
				else if (this.isCheck.length === 0) {
					this.$modal.msg("请点击同意并接受服务协议")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					// setTimeout(() => {
					  this.success()
					// }, 1500)
					//this.$refs.verify.show()
				}
			},
			// 密码登录
			async success(params) {
				//this.loginForm.captchaVerification=params.captchaVerification
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				}).catch(() => {
					// if (this.captchaEnabled) {
					//   this.getCode()
					// }
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				//设置用户信息，调用store里的获取用户信息
				this.$store.dispatch('GetInfo').then(res => {
					uni.setStorageSync('username', this.loginForm.username)
					uni.setStorageSync('password', this.loginForm.password)
					//判断用户类型
					let type = this.$store.getters.userType
					switch (type) {
						case '1': //企业
							//判断是否已经注册工厂
							if (this.$store.getters.companyId == null) {
								this.$tab.reLaunch('/pagesA/factory/index')
							} else {
								wx.switchTab({
									url: '../pages/index'
								})
							}
							break;
						case '2': //员工
							wx.switchTab({
								url: '../pages/index'
							})
							break;
						case '3': //客户
							wx.switchTab({
								url: '../pages/index'
							})
							break;
					}
				})
			}
		}
	}
</script>
<style>
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-image: url('https://fziot.zheyun.top:9021/static/image/backlogin.png');
		background-repeat: no-repeat;
		min-height: 100%;
		background-size: cover;
		height: auto;
	}
</style>
<style lang="scss" scoped>
	.normal-login-container {
		margin-top: 165rpx;
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}
		}

		.easyinput {
			width: 100%;
		}
	}

	.login-code-img {
		height: 45px;
	}

	.regist {
		font-size: 12px;
		margin-top: 8px;
	}

	/deep/ .uni-data-checklist .checklist-group .checklist-box {
		margin-right: 0px
	}
</style>
